package com.rest.demo.functional.concurrent;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author Administrator
 * @decription
 * @Date 2018-01-30 16:17
 */
public class CallableInvokeAny {
    
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        List<Callable<String>> callableList = Arrays.asList(
                callable("task1",2),
                callable("task2",1),
                callable("task3",3)
        );
        ExecutorService executor = Executors.newWorkStealingPool();
        String result = executor.invokeAny(callableList);
        System.out.println(result);
    }

    public static Callable<String> callable(String taskName,long sleepTime) {
        return () -> {
            TimeUnit.SECONDS.sleep(sleepTime);
            return taskName;
        };
    }
}
